<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我要投标</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="demoTable">
    <div class="layui-fluid">
        <div class="layui-row  layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <div class="layui-inline">
                            雇主：
                            <div class="layui-inline">
                                <input type="text" name="acc" placeholder="请输入雇主名字" autocomplete="off"
                                       class="layui-input">
                            </div>
                            需求类型：
                            <div class="layui-input-inline">
                                <select name="ujob" id="type" lay-verify="required" >
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" data-type="reload">查询</button>
                            <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">

                        </div>
                    </div>
                    <div class="layui-card-body ">
                        <table id="demo" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--<div class="demoTable">-->
<!--&lt;!&ndash;    雇主:<input  id="acc">&ndash;&gt;-->
<!--    雇主：-->
<!--    <div class="layui-inline">-->
<!--    <input type="text" name="acc" placeholder="请输入雇主名字" autocomplete="off"-->
<!--           class="layui-input">-->
<!--    </div>-->
<!--&lt;!&ndash;    需求类型:<select name="ujob" lay-verify="required" id="type">&ndash;&gt;-->
<!--    需求类型：-->
<!--    <div class="layui-input-inline">-->
<!--        <select name="ujob" id="type" lay-verify="required" >-->
<!--        </select>-->
<!--    </div>-->
<!--</select>-->
<!--    <button class="layui-btn" data-type="reload">查询</button>-->
<!--    <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">-->
<!--</div>-->

<!--<table id="demo" lay-filter="test"></table>-->

<script id="msg" type="text/html">
    <table>
        <tr>
            <td >
                <span id="uacc">发布者:</span>
            </td>
        </tr>
        <tr>
            <td >
                <span id="title">标题:</span>
            </td>
        </tr>
        <tr>
            <td >
                <span id="text">内容:</span>
            </td>
        </tr>
        <tr>
            <td >
                <span id="requst">需求:</span>
            </td>
        </tr>
        <tr>
            <td >
                <span id="money">佣金:</span>
            </td>
        </tr>
        <tr>
            <td >
                <span id="time">工时:</span>
            </td>
        </tr>
    </table>
    <button class="layui-btn layui-btn-normal" type="button" id="bt">我要投标</button>
    <button class="layui-btn layui-btn-normal" type="button" id="bt2">联盟投标</button>
</script>

<script id="alliance" type="text/html">
    我的联盟:<select  lay-verify="required" id="aname">
    </select>
    <button class="layui-btn layui-btn-normal" type="button" id="yes">确认投标</button>
</script>

<script type="text/html" id="bar">
    {{#  if(d.tips3==='未投标'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">查看详情</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="no">取消投标</a>
    {{#  } }}
</script>

<script>

    $(function ()
    {
	    $.ajax({
		    type:"POST",
		    url:"/api-back/type/getNeed",
		    success:function(msg)
		    {
                document.getElementById("type").length=0;
			    $('#type').append("<option value='所有'>所有</option>");
			    for(var i=0;i<msg.length;i++)
                {
                    $('#type').append("<option value='"+msg[i].sname+"'>"+msg[i].sname+"</option>")
                }
		    },
		    error:function ()
		    {
			    layer.alert("服务器正忙.....", {icon: 5});
		    }
	    });
    });

    function back()
    {
    	window.parent.parent.location.href="/api-front/getPage?pageName=index";
    }

	layui.use('table', function(){
		var table = layui.table;
		table.render({
			elem: '#demo'
            ,type:'POST'
			,url: '/api-front/tender/getList'
			,page: true
			,limit:5
			,limits:[5]
			,id:"testReload"
			,cols: [
				[
				{field: 'nid', hide:true}
				,{field: 'ndemand', hide:true}
				,{field: 'tips2', hide:true}
				,{field: 'ntitle', title: '标题', width:120}
				,{field: 'nuseraccount', title: '发布者', width:80}
                ,{field: 'ncontent', title: '内容', width:120}
                ,{field: 'sname', title: '需求类型', width:120}
                ,{field: 'nmoney', title: '佣金', width:80}
                ,{field: 'nrealtime', title: '项目工期', width:150}
                ,{field: 'nendtime', title: '截止投标日期', width:150}
                ,{field: 'tips3', title: '是否投标', width:80}
				,{title:'操作',width:150,toolbar:"#bar"}
			]
            ]
		});
		$('.demoTable .layui-btn').on('click', function ()
		{
			var type = $(this).data('type');
			if (type == 'reload')
			{
				//执行重载
				table.reload('testReload',
					{
						page:
							{
								curr: 1
							}
						, where:
							{
								acc: $("#acc").val()
                                ,type:$("#type").val()
							}
					});
			}
		});


		table.on('tool(test)', function(obj)
        {
			var data = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;
	        if(layEvent === 'no')
	        {
		        layer.confirm('确定要取消投标吗?', function(index)
		        {
			        $.ajax({
				        type:"POST",
				        url:'/api-front/tender/deleteTender',
				        dataType:"text",
				        data:{nid:data.nid,aid:data.tips2,type:data.tips3},
				        success:function(msg)
				        {
					        if(msg==="yes")
					        {
						        layer.alert("取消成功", {icon: 1});
						        $(".layui-laypage-btn")[0].click();
						        layer.close(index);
					        }
					        else if(msg==="no")
					        {
						        layer.alert("雇主已确认您的投标，无法取消", {icon: 5});
					        }
					        else
                            {
	                            layer.alert("取消失败，请重试", {icon: 5});
                            }
				        },
				        error:function ()
				        {
					        layer.alert("服务器正忙.....", {icon: 5});
				        }
			        });
                });
            }
	        else
            {
	            var str= layer.open({
		            type: 1,
		            content: $('#msg').html(),
		            area: ['250px','200px'],
		            title: '需求详情',
		            success:function () {
			            document.getElementById("uacc").innerText='发布者:'+data.nuseraccount;
			            document.getElementById("title").innerText='标题:'+data.ntitle;
			            document.getElementById("money").innerText='佣金:'+data.nmoney;
			            document.getElementById("text").innerText='内容:'+data.ncontent;
			            document.getElementById("requst").innerText='需求:'+data.ndemand;
			            document.getElementById("time").innerText='工期:'+data.nrealtime;
		            }
	            });
	            $("#bt").click(function ()
	            {
		            layer.confirm('确定要投标吗?', function(index)
		            {
			            $.ajax({
				            type:"POST",
				            url:'/api-front/tender/addTender',
				            dataType:"text",
				            data:{nid:data.nid,ttype:'个人投标'},
				            success:function(msg)
				            {
					            if(msg==="yes")
					            {
						            layer.alert("恭喜你，投标成功", {icon: 1});
						            $(".layui-laypage-btn")[0].click();
						            layer.close(index);
						            layer.close(str);
					            }
					            else
					            {
						            layer.alert("投标截止日期已过，无法投标", {icon: 5});
					            }
				            },
				            error:function ()
				            {
					            layer.alert("服务器正忙.....", {icon: 5});
				            }
			            });
		            });
	            });

	            $("#bt2").click(function ()
	            {
		            $.ajax({
			            type:"POST",
			            url:'/api-front/alliance/getMyAlliance',
			            success:function(msg)
			            {
			            	if(msg.length===0)
                            {
	                            layer.alert("您还没有自己创建的联盟", {icon: 5});
                            }
			            	else {
					           var aaa= layer.open({
						            type: 1,
						            content: $('#alliance').html(),
						            area: ['200px','250px'],
						            title: '我的联盟',
						            success:function () {
							            document.getElementById("aname").length=0;
							            for(var i=0;i<msg.length;i++)
							            {
								            $('#aname').append("<option value='"+msg[i].aid+"'>"+msg[i].aname+"</option>")
							            }
						            }
					            });
					            $("#yes").click(function ()
					            {
						            layer.confirm('确定要投标吗?', function(index)
						            {
						            	var aname=document.getElementById("aname");
						            	var aid=aname.options[aname.selectedIndex].value;
							            $.ajax({
								            type:"POST",
								            url:'/api-front/tender/addTender',
								            dataType:"text",
								            data:{nid:data.nid,aid:aid,ttype:'联盟投标'},
								            success:function(msg)
								            {
									            if(msg==="yes")
									            {
										            layer.alert("恭喜你，投标成功", {icon: 1});
										            $(".layui-laypage-btn")[0].click();
										            layer.close(index);
										            layer.close(aaa);
										            layer.close(str);
									            }
									            else
									            {
										            layer.alert("投标截止日期已过，无法投标", {icon: 5});
									            }
								            },
								            error:function ()
								            {
									            layer.alert("服务器正忙.....", {icon: 5});
								            }
							            });
						            });
                                });
                            }
			            },
			            error:function ()
			            {
				            layer.alert("服务器正忙.....", {icon: 5});
			            }
		            });
	            });
            }
        });
	});
</script>
</body>
</html>